package de.koller.aframe.plain.gui.input;

import de.koller.aframe.util.node.NodeA;
import java.awt.Canvas;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GUIInput extends NodeA {

	private final IInputHandler inputHandler;
	private final Canvas canvas;
	
	public GUIInput( Canvas listento, IInputHandler inputHandler ) {
		this.inputHandler = inputHandler;
		this.canvas = listento;
	}
	
	@Override
	protected void init() {
		super.init();
		
		MouseInput in = new MouseInput();
		Canvas c = canvas;
		c.addMouseListener( in );
		c.addMouseMotionListener( in );
		c.addMouseWheelListener( in );
		c.addKeyListener( new KeyInput() );
	}
	
	private class KeyInput extends KeyAdapter {

		@Override public void keyPressed( final KeyEvent e ) {
			inputHandler.keyPress( e.getKeyCode() );
		}
				
	}

	private class MouseInput extends MouseAdapter {

		@Override public void mousePressed( MouseEvent e ) {
			inputHandler.click( e.getX(), e.getY() );
		}

		@Override
		public void mouseMoved( MouseEvent e ) {
			inputHandler.mouseMove( e.getX(), e.getY() );
		}
		
	}
	
}
